﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using WeatherPlugin;


namespace AHB
{
    public partial class Collection : System.Web.UI.Page
    {
        bool TableContains(string Table, string Field, string Value)
        {
            SqlDataSource1.SelectCommand = "SELECT " + Field + " FROM " + Table + " WHERE " + Field + "='" + Value + "'";
            DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            if (View.Count == 0)
                return false;
            else
                return true;
        }

        int StateFromHiveData(string Site)
        {
            return 0;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            Place weather = new Place();
            weather.Location = "Bushbuckridge, Mpumalanga";
            try
            {
                weather.Update();
            }
            catch { }

            if (Request.QueryString["Type"] == "Register")
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.ContentType = "text/plain";

                Response.Write("OK");
                Response.End();
                return;
            }
            else if (Request.QueryString["Type"] == "Upload")
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "text/plain";

                string Barcode, SiteID;

                Barcode = Request.QueryString["BarcodeID"];
                SiteID = Request.QueryString["SiteID"];

                if (Barcode == null || SiteID == null)
                {
                    Response.Write("AHB-REJECTED");
                    Response.End();
                    return;
                }


                if (Barcode == "" || SiteID == "")
                {
                    Response.Write("AHB-REJECTED");
                    Response.End();
                    return;
                }
                /*
                if (Request.QueryString["GPS"] == null || Request.QueryString["GPS"] == "")
                {
                    Response.Write("AHB-REJECTED");
                    Response.End();
                    return;
                }*/
                try
                {
                    string[] Items;
                    string sLong = "";
                    string sLat = "";
                    Items = Request.QueryString["GPS"].Split('S');

                    if (Items.Length == 2)
                    {

                        sLong = Items[0];
                        sLat = Items[1];

                        sLong = sLong.Replace('P', '.');
                        sLong = sLong.Replace('N', '-');
                        sLat = sLat.Replace('P', '.');
                        sLat = sLat.Replace('N', '-');
                    }

                    string InsertCommand = "INSERT INTO Temp(ID, Barcode, SiteID, SwapBarcode, Rainfall, TempMax, TempMin, WeatherType, Supers, Bees, Capped, Queen, Manipulation, Diseases, Harvested, Comment, VistDateTime, Fed) VALUES(";
                    InsertCommand += "'" + Guid.NewGuid() + "', ";
                    InsertCommand += "'" + Request.QueryString["BarcodeID"] + "', ";
                    InsertCommand += "'" + Request.QueryString["SiteID"] + "', ";
                    InsertCommand += "'" + Request.QueryString["SwapBarcode"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Rain"] + "', ";
                    InsertCommand += "'" + weather.HighTemprature + "', ";
                    InsertCommand += "'" + weather.LowTemprature + "', ";
                    InsertCommand += "'" + weather.CurrentConditions + "', ";
                    InsertCommand += "'" + Request.QueryString["SupersCount"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Bees"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Capped"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Queenright"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Manipulation"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Disease"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Harvest"] + "', ";
                    InsertCommand += "'" + Request.QueryString["Remarks"] + "', ";
                    InsertCommand += "'" + DateTime.Now.ToString() + "', ";
                    if ((Request.QueryString["Fed"] == "null"))
                        InsertCommand += "'" + 0 + "')";
                    else
                        InsertCommand += "'" + (Int32.Parse(Request.QueryString["Fed"]) * 0.5) + "')";

                    SqlDataSource1.InsertCommand = InsertCommand;
                    SqlDataSource1.Insert();

                    if (TableContains("Hives", "Barcode", Request.QueryString["BarcodeID"]) == true)
                    {
                        SqlDataSource1.UpdateCommand = "UPDATE Hives Set SupersCount='" + Request.QueryString["SupersCount"] + "', ";
                        SqlDataSource1.UpdateCommand += "Capped='" + Request.QueryString["Capped"] + "', ";
                        SqlDataSource1.UpdateCommand += "Fed='" + Request.QueryString["Fed"] + "', ";
                        SqlDataSource1.UpdateCommand += "Harvested='" + Request.QueryString["Harvest"] + "', ";
                        SqlDataSource1.UpdateCommand += "Manipulation='" + Request.QueryString["Manipulation"] + "', ";
                        SqlDataSource1.UpdateCommand += "Disease='" + Request.QueryString["Disease"] + "', ";
                        SqlDataSource1.UpdateCommand += "Queen='" + Request.QueryString["Queen"] + "', ";
                        SqlDataSource1.UpdateCommand += "Comment='" + Request.QueryString["Comment"] + "', ";
                        SqlDataSource1.UpdateCommand += "Swap='" + Request.QueryString["SwapBarcode"] + "' ";
                        SqlDataSource1.UpdateCommand += "WHERE Barcode='" + Request.QueryString["BarcodeID"] + "'";
                        SqlDataSource1.Update();
                    }
                    else
                    {
                        string sInsertCommand = "INSERT INTO Hives(Barcode, SiteID, SupersCount, Capped, Fed, Harvested, Manipulation, Disease, Queen, Comment, Swap) VALUES(";
                        sInsertCommand += "'" + Request.QueryString["BarcodeID"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["SiteID"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["SupersCount"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Capped"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Fed"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Harvest"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Manipulation"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Disease"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Queen"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Comment"] + "', ";
                        sInsertCommand += "'" + Request.QueryString["Swap"] + "') ";
                        SqlDataSource1.InsertCommand = sInsertCommand;
                        SqlDataSource1.Insert();
                    }

                    if (TableContains("Sites", "SiteID", Request.QueryString["SiteID"]))
                    {
                        SqlDataSource1.UpdateCommand = "UPDATE Sites SET lastUpdated='" + DateTime.Now.ToString() + "', ";
                        SqlDataSource1.UpdateCommand += "weatherType='" + weather.CurrentConditions + "', ";
                        SqlDataSource1.UpdateCommand += "weatherTemp='" + weather.CurrentTemprature + "', ";
                        if (Items.Length == 2)
                        {
                            SqlDataSource1.UpdateCommand += "poslong='" + sLong + "', ";
                            SqlDataSource1.UpdateCommand += "posLat='" + sLat + "', ";
                        }
                        SqlDataSource1.UpdateCommand += "State='" + StateFromHiveData(Request.QueryString["SiteID"]) + "'";
                        SqlDataSource1.UpdateCommand += " WHERE SiteID='" + Request.QueryString["SiteID"] + "'";
                        SqlDataSource1.Update();
                    }
                    else
                    {
                        SqlDataSource1.InsertCommand = "INSERT INTO Sites(SiteID, posLong, posLat, lastUpdated, weatherType, weatherTemp, beekeeperID, State) VALUES ('";
                        SqlDataSource1.InsertCommand += Request.QueryString["SiteID"] + "', '";
                        SqlDataSource1.InsertCommand += sLong + "', '";
                        SqlDataSource1.InsertCommand += sLat + "', '";
                        SqlDataSource1.InsertCommand += DateTime.Now.ToString() + "', '";
                        SqlDataSource1.InsertCommand += weather.CurrentConditions + "', '";
                        SqlDataSource1.InsertCommand += weather.CurrentTemprature + "','";
                        SqlDataSource1.InsertCommand += "UNKNOWN" + "', '";
                        SqlDataSource1.InsertCommand += StateFromHiveData(Request.QueryString["SiteID"]) + "') ";
                        SqlDataSource1.Insert();
                    }


                }
                catch
                {
                    Response.Write("AHB-REJECTED");
                    Response.End();
                    return;
                }
                Response.Write("AHB-ACCEPTED");
                Response.End();
                return;
            }






            /*

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // If there is a record with the same barcode in Hives Table then update it else, create new record
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            string[] Items;
            string sLong = "";
            string sLat = "";
            Items = Request.QueryString["GPS"].Split('S');

            if (Items.Length == 2)
            {

                sLong = Items[0];
                sLat = Items[1];

                sLong = sLong.Replace('P', '.');
                sLong = sLong.Replace('N', '-');
                sLat = sLat.Replace('P', '.');
                sLat = sLat.Replace('N', '-');
            }

            if (TableContains("Hives", "Barcode", Request.QueryString["BarcodeID"]) == true)
            {
                SqlDataSource1.UpdateCommand = "UPDATE Hives Set SupersCount='" + Request.QueryString["SupersCount"] + "', ";
                SqlDataSource1.UpdateCommand += "Brood='" + Request.QueryString["Brood"] + "', ";
                SqlDataSource1.UpdateCommand += "FullFrames='" + Request.QueryString["FullFrames"] + "', ";
                SqlDataSource1.UpdateCommand += "Feed='" + Request.QueryString["Feed"] + "', ";
                SqlDataSource1.UpdateCommand += "Capped='" + Request.QueryString["Capped"] + "', ";
                SqlDataSource1.UpdateCommand += "Feeding='" + Request.QueryString["Feeding"] + "', ";
                SqlDataSource1.UpdateCommand += "Attention='" + Request.QueryString["Attention"] + "', ";
                SqlDataSource1.UpdateCommand += "Harvest='" + Request.QueryString["Harvest"] + "', ";
                SqlDataSource1.UpdateCommand += "Problems='" + Request.QueryString["Problems"] + "', ";
                SqlDataSource1.UpdateCommand += "Queen='" + Request.QueryString["Queen"] + "' ";
                SqlDataSource1.UpdateCommand += "WHERE Barcode='" + Request.QueryString["BarcodeID"] + "'";
                SqlDataSource1.Update();
            }
            else
            {
                string sInsertCommand = "INSERT INTO Hives(Barcode, SiteID, SupersCount, Brood, FullFrames, Feed, Capped, Feeding, Attention, Harvest, Problems, Queen) VALUES(";
                sInsertCommand += "'" + Request.QueryString["BarcodeID"] + "', ";
                sInsertCommand += "'" + Request.QueryString["SiteID"] + "', ";
                sInsertCommand += "'" + Request.QueryString["SupersCount"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Brood"] + "', ";
                sInsertCommand += "'" + Request.QueryString["FullFrames"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Feed"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Capped"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Feeding"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Attention"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Harvest"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Problems"] + "', ";
                sInsertCommand += "'" + Request.QueryString["Queen"] + "') ";
                SqlDataSource1.InsertCommand = sInsertCommand;
                SqlDataSource1.Insert();
            }


            if (TableContains("Sites", "SiteID", Request.QueryString["SiteID"]))
            {
                SqlDataSource1.UpdateCommand = "UPDATE Sites SET lastupdatedTime='" + DateTime.Now.ToShortTimeString() + "', ";
                SqlDataSource1.UpdateCommand += "lastupdatedDate='" + DateTime.Now.ToShortDateString() + "', ";
                SqlDataSource1.UpdateCommand += "weatherType='" + weather.CurrentConditions + "', ";
                SqlDataSource1.UpdateCommand += "weatherTemp='" + weather.CurrentTemprature + "', ";
                if (Items.Length == 2)
                {
                    SqlDataSource1.UpdateCommand += "poslong='" + sLong + "', ";
                    SqlDataSource1.UpdateCommand += "posLat='" + sLat + "', ";
                }
                SqlDataSource1.UpdateCommand += "State='" + StateFromHiveData(Request.QueryString["SiteID"]) + "'";
                SqlDataSource1.UpdateCommand += " WHERE SiteID='" + Request.QueryString["SiteID"] + "'";
                SqlDataSource1.Update();
            }
            else
            {
                SqlDataSource1.InsertCommand = "INSERT INTO Sites(SiteID, posLong, posLat, lastupdatedTime, lastupdatedDate, weatherType, weatherTemp, beekeeperID, State) VALUES ('";
                SqlDataSource1.InsertCommand += Request.QueryString["SiteID"] + "', '";
                SqlDataSource1.InsertCommand += sLong + "', '";
                SqlDataSource1.InsertCommand += sLat + "', '";
                SqlDataSource1.InsertCommand += DateTime.Now.ToShortTimeString() + "', '";
                SqlDataSource1.InsertCommand += DateTime.Now.ToShortDateString() + "', '";
                SqlDataSource1.InsertCommand += weather.CurrentConditions + "', '";
                SqlDataSource1.InsertCommand += weather.CurrentTemprature + "','";
                SqlDataSource1.InsertCommand += "UNKNOWN" + "', '";
                SqlDataSource1.InsertCommand += StateFromHiveData(Request.QueryString["SiteID"]) + "') ";
                SqlDataSource1.Insert();
            }
        }
             * */
        }
    }
}